package com.jinyi.wwdeng.autodialer.ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jinyi.wwdeng.autodialer.R
import com.jinyi.wwdeng.autodialer.data.CallTask
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class CallRecordAdapter(
    private var dataList: MutableList<CallTask>,
    private val onClickPlay: (View, Int, CallTask) -> Unit) : RecyclerView.Adapter<CallRecordAdapter.ViewHolder>() {

    @SuppressLint("NotifyDataSetChanged")
    fun updateData(list: MutableList<CallTask>?){
        list?.let {
            dataList = list
            notifyDataSetChanged()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addMoreData(list: MutableList<CallTask>?){
        list?.let {
            dataList.addAll(list)
            notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_number_view, parent, false)
        return ViewHolder(view)
    }


    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val model = dataList[position]
        val sb = StringBuilder()
        sb.append("id: ").append(model.id)
        sb.append("\n")
        sb.append("mobile: ").append(model.phoneNumber)
        sb.append("\n")
        sb.append("status: ${model.status.name}")
        sb.append("\n")
        sb.append("createTime: ").append(formatTimestamp(model.createTime))
        sb.append("\n")
        sb.append("callStartTime: ").append(formatTimestamp(model.callStartTime))
        sb.append("\n")
        sb.append("callEndTime: ").append(formatTimestamp(model.callEndTime))
        sb.append("\n")
        sb.append("uploadRetryCount: ").append("${model.uploadRetryCount}")
        sb.append("\n")
        sb.append("reportRetryCount: ").append("${model.reportRetryCount}")
        sb.append("\n")
        sb.append("audioPath: ").append(model.localAudioPath ?: "none")
        sb.append("\n")
        sb.append("audioUrl: ").append(model.serverAudioPath ?: "none")
        sb.append("\n")
        sb.append("product: ").append(model.product ?: "none")
        sb.append("\n")
        sb.append("orderId: ").append(model.orderId ?: "none")
        sb.append("\n")
        sb.append("mobileType: ").append(model.mobileType ?: "none")
        sb.append("\n")
        sb.append("userId: ").append(model.userId ?: "none")
        sb.append("\n")
        sb.append("callLog: ").append(model.callLog ?: "none")
        sb.append("\n")
        sb.append("errorMessage: ").append(model.errorMessage ?: "none")
        holder.tvInfo.text = sb.toString()

        //点击拨号
        holder.tvInfo.setOnClickListener {
            onClickPlay(holder.tvInfo,position, model)
        }

        // 点击播放录音
//        holder.tvInfo.setOnClickListener {
//            if (model.audioPath == null){
//                return@setOnClickListener
//            }
//            onClickPlay(holder.tvInfo,position, model)
//        }

        if (position < itemCount - 1){
            holder.vLine.visibility = View.VISIBLE
        } else {
            holder.vLine.visibility = View.GONE
        }
    }

    override fun getItemCount() = dataList.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvInfo: TextView = itemView.findViewById(R.id.tvInfo)
        val vLine: View = itemView.findViewById(R.id.vLine)
    }

    private fun formatTimestamp(timestamp: Long): String {
        if (timestamp == 0L){
            return "none"
        }
        val sdf = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault())
        return sdf.format(Date(timestamp))
    }
}


